<?php


namespace App\Services\Business\Services;


use App\Services\Business\Interfaces\MediaServiceInterface;

use Carbon\Carbon;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use App\Services\Business\Models\MediaImageables;
use App\Services\Business\Models\MediaVideoables;

class MediaService implements MediaServiceInterface
{





    public function getReferVideoResource(array $videoIds, int $page, int $size)
    {


        $query = MediaVideoables::query()->whereIn('media_video_id', $videoIds);
        $videos = (clone $query)->selectRaw('id,media_video_id,media_videoable_id,media_videoable_type')->with(['video'])->forPage($page, $size)->get()->toArray();
        $total = $query->count();
        $groups = $query
            ->select(['media_videoable_type', DB::raw('GROUP_CONCAT(media_videoable_id) AS resource_ids'), DB::raw('GROUP_CONCAT(media_video_id) AS image_ids')])
            ->whereIn('media_videoable_id', array_column($videos, 'media_videoable_id'))
            ->groupBy('media_videoable_type')
            ->get();


        $allRecords = [
            'combo' => [],
            'anthology' => [],
            'course' => [],
            'group' => [],
            'role'=>[],
        ];
        foreach ($groups as $group) {
            // 获取分组中的 resource_id，转换为数组形式
            $resourceIds = explode(',', $group['resource_ids']);
            // 根据 activity_type 加载对应的模型关联
            $model = $group['media_videoable_type'];

            $resource = new $model();
            // 查询对应的记录
            $records = $resource::query()
                ->select(['*', DB::raw("'" . basename($group['media_videoable_type']) . "' AS type")])
                ->whereIn('id', $resourceIds)
                ->orderBy('created_at', 'desc')
                ->get()
                ->keyBy('id')
                ->toArray();

            $type = strtolower(basename($group['media_videoable_type']));
            foreach ($records as $record) {
                $allRecords[$type][$record['id']] = $record;
            }

        }

        $list = array_map(function ($item) use ($allRecords) {
            $type = strtolower(basename($item['media_videoable_type']));
            $videoId = $item['media_videoable_id'];
            $newFields = [
                'recourse_title' => $allRecords[$type][$videoId]['title'],
                'type' => $type,
            ];
            // 合并原始项和新的键值对
            return array_merge($item, $newFields);

        }, $videos);
        return compact('list', 'total');
    }



    public function getReferImageResource(array $imageIds, int $page, int $size)
    {


        $query = MediaImageables::query()->whereIn('media_image_id', $imageIds);
        $images = (clone $query)->selectRaw('id,media_image_id,media_imageable_id,media_imageable_type')->with(['images'])->forPage($page, $size)->get()->toArray();
        $total = $query->count();
        $groups = $query
            ->select(['media_imageable_type', DB::raw('GROUP_CONCAT(media_imageable_id) AS resource_ids'), DB::raw('GROUP_CONCAT(media_image_id) AS image_ids')])
            ->whereIn('media_imageable_id', array_column($images, 'media_imageable_id'))
            ->groupBy('media_imageable_type')
            ->get();


        $allRecords = [
            'combo' => [],
            'anthology' => [],
            'course' => [],
            'group' => [],
            'role'=>[],
        ];
        foreach ($groups as $group) {
            // 获取分组中的 resource_id，转换为数组形式
            $resourceIds = explode(',', $group['resource_ids']);
            // 根据 activity_type 加载对应的模型关联
            $model = $group['media_imageable_type'];

            $resource = new $model();
            // 查询对应的记录
            $records = $resource::query()
                ->select(['*', DB::raw("'" . basename($group['media_imageable_type']) . "' AS type")])
                ->whereIn('id', $resourceIds)
                ->orderBy('created_at', 'desc')
                ->get()
                ->keyBy('id')
                ->toArray();

            $type = strtolower(basename($group['media_imageable_type']));
            foreach ($records as $record) {
                $allRecords[$type][$record['id']] = $record;
            }

        }

        $list = array_map(function ($item) use ($allRecords) {
            $type = strtolower(basename($item['media_imageable_type']));
            $videoId = $item['media_imageable_id'];
            $newFields = [
                'recourse_title' => $allRecords[$type][$videoId]['title'],
                'type' => $type,
            ];
            // 合并原始项和新的键值对
            return array_merge($item, $newFields);

        }, $images);
        return compact('list', 'total');
    }


    public function hasVideoRefer(array $videoIds)
    {

        return MediaVideoables::query()->WhereIn('media_video_id', $videoIds)->exists();
    }

    public function hasImageRefer(array $imageIds)
    {

        return MediaImageables::query()->WhereIn('media_image_id', $imageIds)->exists();
    }



}
